package com.eric.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eric.bean.CodeBlockDetail;
import com.eric.service.CodeBlockDetailService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eric.bean.CodeBlock;
import com.eric.bean.Student;
import com.eric.form.CodeBlockForm;
import com.eric.service.CodeBlockService;
import com.mysql.jdbc.StringUtils;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.util.calendar.BaseCalendar;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代码块控制器
 * @author 李萌
 *
 */

@Controller
@RequestMapping("/codeBlock")
public class CodeBlockController {
	
	@Resource
	private CodeBlockService codeBlockSer;

	@Resource
	private CodeBlockDetailService codeBlockDetailSer;
	
	@RequestMapping("edit")
	public String CodeBlockEdit(HttpServletRequest request, Model model, HttpServletResponse response){
		String id = request.getParameter("id");
		response.setDateHeader("Expires", 0);
		response.addHeader( "Cache-Control", "no-cache" );//浏览器和缓存服务器都不应该缓存页面信息
		 response.addHeader( "Cache-Control", "no-store" );//请求和响应的信息都不应该被存储在对方的磁盘
		if(! StringUtils.isNullOrEmpty(id)){
			CodeBlock codeBlock = this.codeBlockSer.getCodeBlockById(Integer.parseInt(id));
			model.addAttribute("codeBlock", codeBlock);
		}
		System.out.println("进入编辑页面");
		return "/editCodeBlock";
	}
	
	@RequestMapping("doEdit")
	@ResponseBody
	public Map<String, Object> doEdit(CodeBlockForm codeBlockForm, HttpServletRequest request){
		Map<String,Object> ret = new HashMap<String,Object>();
		CodeBlock coBlock = new CodeBlock();
		
		BeanUtils.copyProperties(codeBlockForm, coBlock);
		
		Integer blockID = codeBlockForm.getId();

		if(null != blockID){
			/*如果是修改,此处其实也可以仅仅是判断数据库中的id与当前id是否相同即可，
			但为了避免程序之前数据库中已经有了很多同名记录，因此我们这样做*/
			List<Map> blocks = this.codeBlockSer.selectListByName(coBlock.getBlockName());
			if(1 < blocks.size()){//记录集超过一个说明除了当前修改的记录本身还包含同名的其他记录
				ret.put("code", 1);
				ret.put("msg", "代码块名称已存在");
				return ret;

			}else{//如果只有一条记录，但是该记录id并不同于前台传来的id，说明有重名的
				if(! blocks.get(0).get("id").equals(coBlock.getId())){
					ret.put("code", 1);
					ret.put("msg", "代码块名称已存在");
					return ret;
				}
			}




			this.codeBlockSer.update(coBlock);

		}else{//如果是新增
			CodeBlock db_block = this.codeBlockSer.selectByName(coBlock.getBlockName());
			if(null != db_block){
				ret.put("code", 1);
				ret.put("msg", "代码块名称已存在");
				return ret;
			}
			this.codeBlockSer.doAdd(request, coBlock);

		}
		

		ret.put("code", 0);
		ret.put("msg", "成功");
		return ret;
		
	}
	@RequestMapping("showCodeList")
	public String showCodeList(Model model){
		
		model.addAttribute("codeList", this.codeBlockSer.getList());
		return "/code_index";
	}
	
	@RequestMapping("delete")
	public String deleteByIds(HttpServletRequest request,Model model){
		String ids = request.getParameter("ids");
		String[] idArr = ids.split(","); 
		
		for(String id : idArr){
//			删除记录
			this.codeBlockSer.delete(id);
//			删除其明细
			this.codeBlockDetailSer.deleteByBlockID(id);
		}

		model.addAttribute("codeList", this.codeBlockSer.getList());
		return "/code_index";
	}
	
}
